﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.TES.Entity.SystemEntity;
using ZDSoft.TES.Core;
using ZDSoft.TES.Service.SystemService;
using ZDSoft.TES.Web.Helpers;
using NHibernate.Criterion;

namespace ZDSoft.TES.Web.Controllers
{
    public class CourseController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public void Create(string CourseName, string IsActive)
        {

            Course course = new Course() { CourseName = CourseName, IsActive = bool.Parse(IsActive) };
            Container.Instance.Resolve<ICourseService>().Create(course);
        }

        public ActionResult Update(int id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Update(string CourseName, string IsActive, int id)
        {
            Course course = Container.Instance.Resolve<ICourseService>().FindByID(id);
            course.CourseName = CourseName;
            course.IsActive = bool.Parse(IsActive);

            Container.Instance.Resolve<ICourseService>().Update(course);
            return View();
        }

        [HttpPost]
        public void Withdraw(int id)
        {
            Course course = Container.Instance.Resolve<ICourseService>().FindByID(id);
            if (course.IsActive == true)
            {
                course.IsActive = false;
            }
            else
            {
                course.IsActive = true;
            }
            Container.Instance.Resolve<ICourseService>().Update(course);
        }
        public string GetCourseByID(int id)
        {
            Course course = Container.Instance.Resolve<ICourseService>().FindByID(id);
            IList<KeyValuePair<string, InvokeMethod>> list = new List<KeyValuePair<string, InvokeMethod>>();
            //list.Add(new KeyValuePair<string, InvokeMethod>("xx", GetUsers));
            string str = course.ToJsonString<Course>(list);
            return str;
        }

        public JsonResult GetPaged(int pageIndex, int pageSize, string strSearchProperty,
          string strSearchCondition, string strSortProperty, string strSortCondition)
        {
            IList<ICriterion> queryConditions = new List<ICriterion>();
            IList<Order> orderList = new List<Order>();
            if (strSearchProperty != "")
            {
                string[] searchProperties = strSearchProperty.Split(',');
                string[] searchConditions = strSearchCondition.Split(',');
                queryConditions = new List<ICriterion>();
                for (int i = 0; i < searchProperties.Length; i++)
                {
                    queryConditions.Add(Expression.Eq(searchProperties[i], searchConditions[i]));
                }
            }
            if (strSortProperty != "")
            {
                string[] sortProperties = strSortProperty.Split(',');
                string[] sortConditions = strSortCondition.Split(',');
                orderList = new List<Order>();
                for (int i = 0; i < sortProperties.Length; i++)
                {
                    if (sortConditions[i] == "asc")
                    {
                        orderList.Add(new Order(sortProperties[i], true));
                    }
                    else
                    {
                        orderList.Add(new Order(sortProperties[i], false));
                    }
                }
            }

            int count;
            int pageNum;

            IList<Course> list = new List<Course>();
            IList<Course> courses = Container.Instance.Resolve<ICourseService>().GetPaged(queryConditions, orderList, pageIndex, pageSize, out count);

            int tmpcountI = count / pageSize;
            float temCountF = float.Parse(count.ToString()) / float.Parse(pageSize.ToString());
            if (temCountF > float.Parse(tmpcountI.ToString()))
            {
                pageNum = tmpcountI + 1;
            }
            else
            {
                pageNum = tmpcountI;
            }
            foreach (Course course in courses)
            {
                Course courseJson = course.ToJaonResult<Course>();
                list.Add(courseJson);
            }
            list.Add(new Course() { ID = pageNum });
            JsonResult result = this.Json(list, JsonRequestBehavior.AllowGet);
            return result;
        }
    }
}



